/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rcs.adslmonitor.routers;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.rcs.adslmonitor.ADSLStatusFromTelnet;

/**
 *
 * @author ruben
 */
public class TPLink8970 extends ADSLStatusFromTelnet {
  public TPLink8970() {
    super("Tp-LINK 8970", "TP-LINK(conf)#", "logout") ;
    
   setUser("admin");
   setPassword("admin");
   setRouterURL("telnet://192.168.1.1/");
  }
  
  protected String parseAsDouble(String value) {
    double result = Double.parseDouble(value) ;
    
    return (result / 10) + "" ;
  }
  
  protected void tryParseLine(String line) {
    String[] tokens = line.split("=") ;
    
    if (null != tokens && tokens.length == 2) {
      if (tokens[0].equalsIgnoreCase("status"))
        put(KEYS.ADSL_LINE_STATUS, tokens[1]);
      else if (tokens[0].equalsIgnoreCase("modulationType"))
        put(KEYS.ADSL_MODE, tokens[1]) ;
      else if (tokens[0].equalsIgnoreCase("upstreamCurrRate"))
        put(KEYS.UP_STREAM, tokens[1]) ;
      else if (tokens[0].equalsIgnoreCase("downstreamCurrRate"))
        put(KEYS.DOWN_STREAM, tokens[1]) ;
      else if (tokens[0].equalsIgnoreCase("upstreamMaxRate"))
        put(KEYS.ATTAINABLE_UP_RATE, tokens[1]) ;
      else if (tokens[0].equalsIgnoreCase("downstreamMaxRate"))
        put(KEYS.ATTAINABLE_DOWN_RATE, tokens[1]) ;
      else if (tokens[0].equalsIgnoreCase("upstreamNoiseMargin"))
        put(KEYS.SNR_MARGIN_UP_STREAM, parseAsDouble(tokens[1])) ;
      else if (tokens[0].equalsIgnoreCase("downstreamNoiseMargin"))
        put(KEYS.SNR_MARGIN_DOWN_STREAM, parseAsDouble(tokens[1])) ;
      else if (tokens[0].equalsIgnoreCase("upstreamAttenuation"))
        put(KEYS.ATTENUATION_UP_STREAM, parseAsDouble(tokens[1])) ;
      else if (tokens[0].equalsIgnoreCase("downstreamAttenuation"))
        put(KEYS.ATTENUATION_DOWN_STREAM, parseAsDouble(tokens[1])) ;
      
//      tokens[0].equalsIgnoreCase("upstreamCurrRate") ;
//      downstreamCurrRate
//      downstreamMaxRate
//      upstreamMaxRate
//                      
//      upstreamNoiseMargin=190
//      downstreamNoiseMargin=63
//      upstreamAttenuation=315
//      downstreamAttenuation=552
//        
//      status=Up
//      modulationType=ADSL_G.dmt
    }
  }
  
   @Override
  protected boolean onRefreshCmds() {
    boolean result = false ;
    try {
      sendCommand("adsl", "show", "info");
      String line = readLineUntil("cmd:SUCC") ;
      
      while (null != line) {
        tryParseLine(line);
        line = readLineUntil("cmd:SUCC") ;
      }
      
      result = true ;
    } catch (IOException ex) {
      Logger.getLogger(TPLink8970.class.getName()).log(Level.SEVERE, null, ex);
    }
    
    return result ;
  }
  
  public static void main(String[] args) {
    try {
      TPLink8970 status = new TPLink8970() ;
      
      status.setUser("admin");
      status.setPassword("admin");
      status.setRouterURL(new URL("telnet://192.168.1.1/"));
      
      status.refresh() ;
      
    } catch (MalformedURLException ex) {
      Logger.getLogger(TPLink8970.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}
